package com.jmc.calls;


import java.io.Serializable;
import java.sql.SQLException;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.CommandCall;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;

public class TestProgramCall implements Serializable{

	public static void main(String[] args) throws SQLException{
		// Call programs on system "Hal"
		AS400 as400 = new AS400("192.168.1.1", "jmc", "jmc");
		ProgramCall pgm = new ProgramCall( as400 );
		try
		{
		//Agrego la Libreria de Datos
		CommandCall cmd = new CommandCall( as400 );
		if (cmd.run("ADDLIBLE libivasd1")!=true)
		{
		// Note that there was an error
		System.out.println( "program failed!" );
		}
		// Show the messages (returned whether or not there was an error)
		AS400Message[] messagelistCommand = cmd.getMessageList();
		for (int i=0; i < messagelistCommand.length; i++)
		{
		// show each message
		System.out.println( messagelistCommand[i].getText() );
		}
			
		// Initialize the name of the program to run
		String progName = "/QSYS.LIB/COINSEUTIL.LIB/PDFQSND.PGM";
		// Setup the 3 parameters
		ProgramParameter[] parmlist = new ProgramParameter[1];
		// First parameter is to input a name
		AS400Text nametext = new AS400Text(8);
		
		parmlist[0] = new ProgramParameter(nametext.toBytes("JJ") );
		
		pgm.setProgram( progName, parmlist );
		// Run the program
		if (pgm.run()!=true)
		{
		// Note that there was an error
		System.out.println( "program failed!" );
		// Show the messages
		AS400Message[] messagelist = pgm.getMessageList();
		for (int i=0; i < messagelist.length; i++)
		{
		// show each message
		System.out.println( messagelist[i] );
		}
		}
		else
		{
		AS400Text text = null;
		}
		}
		catch (Exception e)
		{
		System.out.println( "Program " + pgm.getProgram() + " did not run!" );
		e.printStackTrace();
		}
		// done with the system
		as400.disconnectAllServices();

	
	}
}
